home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Ahoy 1985 June
/
Ahoy_Magazine_85-06_1985_Double_L.d64
/
towers of hanoi
(
.txt
)
< prev
next >
Wrap
Commodore BASIC
|
2022-10-26
|
8KB
|
260 lines
10 rem - towers of hanoi for commodore 64
20 rem - daniel miller
30 rem - 2815 34 street
40 rem - astoria, queens
50 rem - new york, new york 11103
60 rem - september 1, 1983
70 rem ********************************
80 goto2150
90 poke53281,1:zz=1:printcl$;left$(do$,2);tab(11);hdg$;
100 poke55587,10:poke1315,49:poke55595,10:poke1323,50:poke55603,10:poke1331,51
110 print left$(do$,20);tab(5);a$(1);left$(do$,9);tab(11);a$(3);
120 printleft$(do$,9);tab(19);a$(3);left$(do$,9);tab(27);a$(3)
130 rem - place sprites on tower #1
140 pokev+14,93:pokev+15,160:pokev+12,93:pokev+13,151:pokev+10,93:pokev+11,142
150 pokev+8,93:pokev+9,133:pokev+6,93:pokev+7,124:pokev+4,93:pokev+5,115
160 pokev+2,93:pokev+3,106:pokev+0,93:pokev+1,97
170 if yn$="y" thenn2=8-num:goto200
180 if zz=0 then350
190 printleft$(do$,22);tab(8);num$;:input num:n2=8-num:sp=0:if num=8 then270
200 if num<2 or num>8 thenprint left$(do$,22);tab(5);sp$;:goto190
210 if num=8 and yn$="y" then270
220 n4=n2
230 for x=1 to n4
240 sp=sp+(2^(n4-1))
250 n4=n4-1:next x
260 pokev+21,(255-sp)
270 zz=0:for tm=1 to 250:next tm:printleft$(do$,22);tab(5);sp$;
280 t1$="":t2$="":t3$="":n3=n2+1
290 for x=1 to num:t1$=t1$+t4$(x):t2$=t2$+t5$(x):t3$=t3$+t6$(x):next x
300 if yn$="y" then1430
310 for x=1 to num:y=2^x-1:next x
320 num$=rv$+g2$+str$(num)+ro$+slv$(1)+rv$+g2$+str$(y)+slv$(2):y0=y
330 printleft$(do$,22);tab(6);num$;:for tm=1 to 2000:next tm
340 for tm=1 to 250:next tm:printleft$(do$,22);tab(5);sp$;
350 printleft$(do$,22);tab(6);f9$;" ";tab(24);t9$;" ";
360 printleft$(do$,22);tab(6);f9$;" ";cr$;
370 num$="":slv$(1)="":slv$(2)=""
380 if za=2 then420
390 get twr$(1):if twr$(1)="" then390
400 if twr$(1)="[133]" then1330:rem - 'f1'
410 if (twr$(1)<"1" or twr$(1)>"3") thentwr$(1)="":goto390
420 print rv$+red$+twr$(1)+ro$;
430 gosub520
440 printleft$(do$,22);tab(34);" ";cr$;
450 if za=2 then480
460 get twr$(2):if twr$(2)="" then460
470 if twr$(2)<"1" or twr$(2)>"3" thentwr$(2)="":goto460
480 print rv$+red$+twr$(2)+ro$;
490 if twr$(1)<>twr$(2) then510
500 er$=rv$+red$+" invalid entries"+ro$:goto610
510 goto640
520 x=1:gosub530:return
530 on val(twr$(x)) goto540,560,580
540 if val(t1$)=0 then600
550 return
560 if val(t2$)=0 then600
570 return
580 if val(t3$)=0 then600
590 return
600 er$=rv$+red$+"tower # "+twr$(x)+" is empty"+ro$
610 for y=1 to 5:printleft$(do$,24);tab(12)er$;:for tm=1 to 250:next tm
620 printleft$(do$,24);tab(12);left$(sp$,18);:for tm=1 to 250
630 next tm,y:er$="":goto350
640 on val(twr$(1)) goto650,680,710
650 for x=1 to num:if mid$(t1$,x,1)="1" then670
660 next x:loc(1)=160:goto740
670 loc(1)=t1(n2+x):t4$(n2+x)="0":goto740
680 for x=1 to num:if mid$(t2$,x,1)="1" then700
690 next x:loc(1)=160:goto740
700 loc(1)=t2(n2+x):t5$(n2+x)="0":goto740
710 for x=1 to num:if mid$(t3$,x,1)="1" then730
720 next x:loc(1)=160:goto740
730 loc(1)=t3(n2+x):t6$(n2+x)="0"
740 lc=loc(1):gosub880:sp(1)=sp
750 on val(twr$(2)) goto760,790,820
760 for x=1 to num:if mid$(t1$,x,1)="1" then780
770 next x:loc(2)=160:t4$(n2+x-1)="1":goto850
780 loc(2)=t1(n2+x-1):t4$(n2+x-1)="1":goto850
790 for x=1 to num:if mid$(t2$,x,1)="1" then810
800 next x:loc(2)=160:t5$(n2+x-1)="1":goto850
810 loc(2)=t2(n2+x-1):t5$(n2+x-1)="1":goto850
820 for x=1 to num:if mid$(t3$,x,1)="1" then840
830 next x:loc(2)=160:t6$(n2+x-1)="1":goto850
840 loc(2)=t3(n2+x-1):t6$(n2+x-1)="1"
850 lc=loc(2):gosub880:sp(2)=sp
860 t1$="":t2$="":t3$="":n5=n2+1
870 for x=n5 to 8:t1$=t1$+t4$(x):t2$=t2$+t5$(x):t3$=t3$+t6$(x):next x:goto960
880 if lc=97 thensp=1:return
890 if lc=106 thensp=2:return
900 if lc=115 thensp=3:return
910 if lc=124 thensp=4:return
920 if lc=133 thensp=5:return
930 if lc=142 thensp=6:return
940 if lc=151 thensp=7:return
950 if lc=160 thensp=8:return
960 on val(twr$(1)) goto970,980,990
970 sq%=r1%(1,sp(1)):r1%(1,sp(1))=8:goto1000
980 sq%=r2%(1,sp(1)):r2%(1,sp(1))=8:goto1000
990 sq%=r3%(1,sp(1)):r3%(1,sp(1))=8
1000 on val(twr$(2)) goto1010,1020,1030
1010 r1%(1,sp(2))=sq%:goto1040
1020 r2%(1,sp(2))=sq%:goto1040
1030 r3%(1,sp(2))=sq%
1040 on val(twr$(1)) goto1050,1060,1070
1050 x1=93:goto1080
1060 x1=157:goto1080
1070 x1=222
1080 on val(twr$(2)) goto1090,1100,1110
1090 x2=93:goto1120
1100 x2=157:goto1120
1110 x2=222
1120 rem - move sprite
1130 on sq%+1 goto1140,1150,1160,1170,1180,1190,1200,1210
1140 sp=0:sq=1:goto1220
1150 sp=2:sq=3:goto1220
1160 sp=4:sq=5:goto1220
1170 sp=6:sq=7:goto1220
1180 sp=8:sq=9:goto1220
1190 sp=10:sq=11:goto1220
1200 sp=12:sq=13:goto1220
1210 sp=14:sq=15
1220 if val(twr$(1))>val(twr$(2)) then1260
1230 for yy=loc(1) to 55 step-1:poke v+sp,x1:poke v+sq,yy:next yy
1240 for xx=x1 to x2:poke v+sp,xx:poke v+sq,55:next xx
1250 for yy=55 to loc(2):poke v+sp,x2:poke v+sq,yy:next yy:goto1290
1260 for yy=loc(1) to 55 step-1:poke v+sp,x1:poke v+sq,yy:next yy
1270 for xx=x1 to x2 step-1:poke v+sp,xx:poke v+sq,55:next xx
1280 for yy=55 to loc(2):pokev+sp,x2:pokev+sq,yy:next yy
1290 mv=mv+1:printleft$(do$,24);tab(17);mv$+rv$+b2$+str$(mv)+ro$;
1300 if za=2 thenmw=mw+1:goto1440:rem - computer's next move
1310 goto350:rem - player's next move
1320 rem - computer solution
1330 twr$(1)="":printleft$(do$,24);tab(7);cmp$;
1340 get yn$:if yn$="" then1340
1350 if yn$="y" then1370
1360 if yn$<>"n" thenyn$="":goto1330
1370 print rv$+bl$+" "+yn$+ro$;:for tm=1 to 500:next tm:if yn$="y" then1420
1380 for tm=1 to 1000:next tm
1390 printleft$(do$,24);tab(7);sp$;left$(do$,24);tab(9);en$;
1400 get rt$:if rt$<>chr$(13) then1400
1410 poke v+21,0:print cl$;:end
1420 sp=0:mv=0:mw=1:goto1990
1430 yn$="":za=2:printleft$(do$,24);tab(7);sp$;
1440 bi$="":for x=1 to 8:bi$(x)="0":next x
1450 if mv=y0 then1380
1460 ct=fre(0)
1470 c2=0:c3=0:c4=0:c5=0:c6=0:za=2
1480 dec=mw
1490 for y=0 to 8:if int(2^y)>dec theny=y-1:bi$(y)="1":goto1510
1500 bi$(y)="0":next y
1510 dec=dec-2^y:if dec=0 then1530
1520 goto1490
1530 for y=7 to 0 step-1:bi$=bi$+bi$(y):next y
1540 for x=8 to 1 step-1:if mid$(bi$,x,1)="0" thenc2=c2+1:next x
1550 c2=c2+1
1560 ri%=c2+num-1:on num-1 goto1570,1580,1590,1600,1610,1620,1630
1570 ri%=ri%+4:goto1640
1580 ri%=ri%+2:goto1640
1590 goto1640
1600 ri%=ri%-2:goto1640
1610 ri%=ri%-4:goto1640
1620 ri%=ri%-6:goto1640
1630 ri%=abs(ri%-8)
1640 for x=1 to 8:if r1%(1,x)=ri% thentwr$(1)="1":goto1670
1650 next x:for x=1 to 8:if r2%(1,x)=ri% thentwr$(1)="2":goto1670
1660 next x:twr$(1)="3"
1670 l$=twr$(1)
1680 for x=1 to 8:if mid$(bi$,x,1)="1" thenc3=c3+1:next x:goto1700
1690 next x
1700 if c3>1 then1740
1710 if val(t1$)=0 thentwr$(2)="1":goto350
1720 if val(t2$)=0 thentwr$(2)="2":goto350
1730 if val(t3$)=0 thentwr$(2)="3":goto350
1740 for x=8 to 1 step-1:c4=c4+1:if mid$(bi$,x,1)="0" thennext x
1750 c6=c4
1760 for x=8-c4 to 1 step-1:c6=c6+1:if mid$(bi$,x,1)="0"thenc5=c5+1:next x
1770 rj%=c6+num-1:on num-1 goto1780,1790,1800,1810,1820,1830,1840
1780 rj%=rj%+4:goto1850
1790 rj%=rj%+2:goto1850
1800 goto1850
1810 rj%=rj%-2:goto1850
1820 rj%=rj%-4:goto1850
1830 rj%=rj%-6:goto1850
1840 rj%=abs(rj%-8)
1850 for x=1 to 8:if r1%(1,x)=rj% thentx$="1":goto1880
1860 next x:for x=1 to 8:if r2%(1,x)=rj% thentx$="2":goto1880
1870 next x:tx$="3"
1880 m$=tx$
1890 lm$=l$+m$
1900 if (c5/2)=int(c5/2) or c5=0 thentwr$(2)=tx$:goto1970
1910 if lm$="12" thentwr$(2)="3":goto1970
1920 if lm$="13" thentwr$(2)="2":goto1970
1930 if lm$="23" thentwr$(2)="1":goto1970
1940 if lm$="21" thentwr$(2)="3":goto1970
1950 if lm$="31" thentwr$(2)="2":goto1970
1960 if lm$="32" thentwr$(2)="1"
1970 l$="":m$="":lm$="":ct=fre(0):goto350
1980 rem - build sprites
1990 v=53248:pokev+21,255:pokev+23,255:pokev+29,255
2000 poke2040,192:poke2041,193:poke2042,194:poke2043,195:poke2044,196
2010 poke2045,197:poke2046,198:poke2047,199
2020 pokev+39,3:pokev+40,15:pokev+41,13:pokev+42,9:pokev+43,14:pokev+44,5
2030 poke v+45,8:pokev+46,0
2040 restore:n0=12288:for x=1 to 8:for n=0 to 62:read q:poke n0+n,q:next n
2050 n0=n0+64:next x
2060 t1$="11111111":t2$="00000000":t3$="00000000"
2070 for x=1 to 8:t4$(x)=mid$(t1$,x,1):next x
2080 for x=1 to 8:t5$(x)=mid$(t2$,x,1):next x
2090 for x=1 to 8:t6$(x)=mid$(t3$,x,1):next x
2100 z%=97:for x=1 to 8:t1(x)=z%:t2(x)=z%:t3(x)=z%:z%=z%+9:next x
2110 y%=0:for x=1 to 8:r1%(1,x)=y%:y%=y%+1:next x
2120 for x=1 to 8:r2%(1,x)=9:r3%(1,x)=9:next x
2130 if yn$="y" then140
2140 goto90
2150 rem - build character graphics
2160 blk$=chr$(144):red$=chr$(28):yel$=chr$(158):b2lu$=chr$(31):cy$=chr$(159)
2170 g1rey$="[151]":g2rey$="[152]":rem - c=(4); c=(5)
2180 rv$=chr$(18):ro$=chr$(146)
2190 cl$=chr$(147):dw$=chr$(17):lf$=chr$(157)
2200 for x=1 to 24:do$=do$+dw$:next x:do$=chr$(19)+do$
2210 a$(1)=rv$+b2$+" "+ro$
2220 a$(2)=rv$+yel$+" "+dw$+lf$
2230 for x=1 to 10:a$(3)=a$(3)+a$(2):next x
2240 a$(3)=a$(3)+" "+ro$
2250 hdg$=rv$+wh$+"towers of hanoi"+ro$
2260 num$=rv$+b2$+"number of rings (2 - 8)"+ro$
2270 sp$=" "
2280 f9$=rv$+g1$+"from tower #"+ro$:t9$=rv$+g1$+"to tower #"+ro$
2290 cr$=rv$+cy$+" "+ro$
2300 slv$(1)=rv$+g2$+" rings requires"+ro$:slv$(2)=rv$+g2$+" moves."+ro$
2310 mv$=rv$+b2$+"move #"+ro$
2320 cmp$=rv$+g1$+"computer solution (y/n) ?"+ro$
2330 en$=rv$+g1$+"press <return> to end"+ro$
2340 goto1990
2350 rem - data for sprites
2360 rem - sprite 0
2370 data ,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,:rem 53 commas
2380 data 1,255,128,1,255,128,1,255,128
2390 rem - sprite 1
2400 data ,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
2410 data 3,255,192,3,255,192,3,255,192
2420 rem - sprite 2
2430 data ,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
2440 data 7,255,224,7,255,224,7,255,224
2450 rem - sprite 3
2460 data ,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
2470 data 15,255,240,15,255,240,15,255,240
2480 rem - sprite 4
2490 data ,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
2500 data 31,255,248,31,255,248,31,255,248
2510 rem - sprite 5
2520 data ,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
2530 data 63,255,252,63,255,252,63,255,252
2540 rem - sprite 6
2550 data ,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
2560 data 127,255,254,127,255,254,127,255,254
2570 rem - sprite 7
2580 data ,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
2590 data 255,255,255,255,255,255,255,255,255